import {
    _decorator,
    Animation,
    BoxCollider,
    CapsuleCollider,
    Component,
    CylinderCollider,
    ICollisionEvent,
    ITriggerEvent,
    MeshCollider,
    Quat,
} from 'cc';
import {ColliderGroupEnum} from "db://assets/scripts/game/ColliderGroupEnum";
import {PriorityEnum} from "db://assets/scripts/game/PriorityEnum";
import {GameManager} from "db://assets/scripts/game/GameManager";
import {UIManager} from "db://assets/scripts/common/UIManager";
import {PoolManager} from "db://assets/scripts/common/PoolManager";
import {PlayerAnimationType} from "db://assets/scripts/game/role/player/PlayerAnimationType";
import {PlayerActionEnum} from "db://assets/scripts/game/role/player/PlayerActionEnum";
import {GameState} from "db://assets/scripts/game/GameState";
import {PlayerSkillManager} from "db://assets/scripts/game/role/player/PlayerSkillManager";

const {ccclass, property} = _decorator;

/**
 * 碰撞体名称
 */
enum COLLIDER_NAME {
    /**
     * 大爱心, 玩家吃到后增加生命上限
     */
    HEART_BIG = 1,
    /**
     * 传送门
     */
    WARP_GATE = 2,
    /**
     * NPC商人
     */
    NPC_BUSINESS_MAN = 3,
    /**
     * NPC智慧老头
     */
    NPC_WISE_MAN = 4,
}

/**
 * 管理游戏若干个碰撞器控制器
 */
@ccclass('ColliderItemController')
export class ColliderItemController extends Component {
    /**
     * 碰撞体类型名称
     */
    @property({displayOrder: 1})
    public colliderName: COLLIDER_NAME = COLLIDER_NAME.HEART_BIG;

    /**
     * 碰撞体
     */
    public colliderCom: any;
    /**
     * 动画
     */
    public ani: Animation;

    /**
     * 碰撞体名称
     */
    public static COLLIDER_NAME = COLLIDER_NAME;

    /**
     * 爱心旋转
     */
    private currentHeartBigQuat: Quat = new Quat();
    /**
     * 定时器
     */
    private timer: any = null;

    onLoad() {
        this.colliderCom = this.node.getComponent(BoxCollider)
            || this.node.getComponent(CylinderCollider)
            || this.node.getComponent(CapsuleCollider)
            || this.node.getComponent(MeshCollider);

        if (!this.colliderCom) {
            console.error("this node does not have collider component");
        }
    }

    onEnable() {
        if (this.colliderCom.isTrigger) {
            this.colliderCom.on('onTriggerEnter', this._onTriggerEnterCb, this);
        } else {
            this.colliderCom.on('onCollisionEnter', this._onCollisionEnterCb, this);
        }
    }

    onDisable() {
        if (this.colliderCom.isTrigger) {
            this.colliderCom.off('onTriggerEnter', this._onTriggerEnterCb, this);
        } else {
            this.colliderCom.off('onCollisionEnter', this._onCollisionEnterCb, this);
        }
    }

    start() {

    }

    update(deltaTime: number) {
        if (GameManager.getInstance().gameStateProxy.getGameState() === GameState.OVER || !GameManager.getInstance().gamePlayerProxy.getPlayerNode() || !this.node.parent) {
            return;
        }

        if (this.colliderName === COLLIDER_NAME.HEART_BIG) {
            Quat.fromEuler(this.currentHeartBigQuat, 0, 120 * deltaTime, 0);
            this.node.rotate(this.currentHeartBigQuat);
        }
    }

    public setTimer(obj: any) {
        if (this.timer) {
            clearTimeout(this.timer);
            this.timer = null;
        }
    }

    private _onTriggerEnterCb(event: ITriggerEvent) {
        this._hitTarget(event.otherCollider, event.selfCollider);
    }

    private _onCollisionEnterCb(event: ICollisionEvent) {
        this._hitTarget(event.otherCollider, event.selfCollider);
    }

    private _hitTarget(otherCollider: any, selfCollider: any) {
        if (GameManager.getInstance().gameStateProxy.getGameState() === GameState.OVER
            || GameManager.getInstance().gameStateProxy.getGameState() !== GameState.PLAYING) {
            return;
        }

        // console.log("getGroup", otherCollider.getGroup());

        if (otherCollider.getGroup() == ColliderGroupEnum.PLAYER && GameManager.getInstance().gamePlayerProxy.getPlayerNode()) {
            switch (this.colliderName) {
                case COLLIDER_NAME.HEART_BIG:
                    GameManager.getInstance().gamePlayerProxy.getPlayerController().addBlood(300);
                    PoolManager.getInstance().putNode(this.node);
                    GameManager.getInstance().gameWarpGateProxy.checkTriggerAll();
                    break;
                case COLLIDER_NAME.WARP_GATE:
                    GameManager.getInstance().gamePlayerProxy.getPlayerController().playAction({action: PlayerActionEnum.STOP_MOVE});
                    GameManager.getInstance().gamePlayerProxy.getPlayerController().characterRigidController.stopMove();
                    GameManager.getInstance().gamePlayerProxy.getPlayerNode().active = false;
                    GameManager.getInstance().gameStateProxy.setWin(true);
                    break;
                case COLLIDER_NAME.NPC_BUSINESS_MAN:
                    GameManager.getInstance().gameStateProxy.setGameState(GameState.PAUSE);
                    GameManager.getInstance().gamePlayerProxy.getPlayerController().characterRigidController.stopMove();
                    GameManager.getInstance().gamePlayerProxy.getPlayerController().scriptPlayerModel.playAni(PlayerAnimationType.IDLE, true);
                    if (PlayerSkillManager.getInstance().isUnlockAllSkill()) {
                        //防错
                        UIManager.getInstance().showTip("所有技能均已解锁");
                        PoolManager.getInstance().putNode(this.node);
                        GameManager.getInstance().gameStateProxy.setGameState(GameState.PAUSE);
                    } else {
                        UIManager.getInstance().hideDialog("fight/fightPanel");
                        UIManager.getInstance().showDialog("shop/shopPanel", [() => {
                            GameManager.getInstance().gameStateProxy.setGameState(GameState.PAUSE);
                            PoolManager.getInstance().putNode(this.node);
                        }], () => {
                        }, PriorityEnum.DIALOG);
                    }

                    GameManager.getInstance().gameWarpGateProxy.checkTriggerAll();
                    break;
                case COLLIDER_NAME.NPC_WISE_MAN:
                    GameManager.getInstance().gameStateProxy.setGameState(GameState.PAUSE);
                    GameManager.getInstance().gamePlayerProxy.getPlayerController().characterRigidController.stopMove();
                    GameManager.getInstance().gamePlayerProxy.getPlayerController().scriptPlayerModel.playAni(PlayerAnimationType.IDLE, true);
                    if (PlayerSkillManager.getInstance().isUnlockAllSkill()) {
                        UIManager.getInstance().showTip("所有技能均已解锁");
                        PoolManager.getInstance().putNode(this.node);
                        GameManager.getInstance().gameStateProxy.setGameState(GameState.PAUSE);
                    } else {
                        UIManager.getInstance().hideDialog("fight/fightPanel");
                        UIManager.getInstance().showDialog("skill/skillPanel", [() => {
                            PoolManager.getInstance().putNode(this.node);
                            GameManager.getInstance().gameStateProxy.setGameState(GameState.PAUSE);
                        }], () => {
                        }, PriorityEnum.DIALOG);
                    }

                    GameManager.getInstance().gameWarpGateProxy.checkTriggerAll();
                    break;
            }
        }
    }
}

